package com.changgou.order.service.impl;

import com.changgou.goods.feign.SkuFeign;
import com.changgou.goods.feign.SpuFeign;
import com.changgou.goods.pojo.Sku;
import com.changgou.goods.pojo.Spu;
import com.changgou.order.pojo.OrderItem;
import com.changgou.order.service.CartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 购物车功能实现
 */
@Service
public class CartServiceImpl implements CartService {
    //定义redis前缀   存储key以cart_username的形式储存
    private static final String CART = "cart_";

    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private SkuFeign skuFeign;
    @Autowired
    private SpuFeign spuFeign;

    /**
     * 购物车添加功能
     * @param skuId
     * @param num
     * @param username
     */
    @Override
    public void add(String skuId, Integer num, String username) {
//        查询redis中对应商品信息
        OrderItem orderItem = (OrderItem) redisTemplate.boundHashOps(CART + username).get(skuId);
        if (orderItem!=null){
            //如果已有商品信息 则数量做增加操作
            orderItem.setNum(orderItem.getNum()+num);
            if (orderItem.getNum()<=0){
                //删除该商品
                redisTemplate.boundHashOps(CART+username).delete(skuId);
                return;//结束方法。
            }
            //设置商品总价
            orderItem.setMoney(orderItem.getNum()*orderItem.getPrice());
            //设置商品需要付的价格
            orderItem.setPayMoney(orderItem.getNum()*orderItem.getPrice());

        }else {
            //如果购物车商品不存在 把商品添加到redis
            Sku sku = skuFeign.findById(skuId).getData();
            Spu spu = spuFeign.findSpuById(sku.getSpuId()).getData();
            //封装orderItem
            orderItem=this.sku2OrderItem(sku, spu, num);
        }
        //把orderItem存入redis
        redisTemplate.boundHashOps(CART+username).put(skuId,orderItem);
    }
    /**
     * 获取购物车列表数据
     * @param username
     * @return
     */
    @Override
    //通过令牌中动态获取的用户名 查看购物车列表
    public Map list(String username) {
        //用map集合封装
        Map map=new HashMap();
        //通过redisTemplate 的values方法 获取该用户下的所有商品列表
        List<OrderItem> orderItemList = redisTemplate.boundHashOps(CART + username).values();
        //商品集合存入map
        map.put("orderItemList",orderItemList);
        //商品的数量和总价
        Integer totalNum=0;
        Integer totalPrice=0;
        //获取商品的总价和总数
        for (OrderItem orderItem : orderItemList) {
            totalNum+=orderItem.getNum();
            totalPrice+=orderItem.getMoney();
        }
        map.put("totalNum",totalNum);
        map.put("totalPrice",totalPrice);
        return map;
    }

    private OrderItem sku2OrderItem(Sku sku, Spu spu, Integer num) {
        //新建商品列表
        OrderItem orderItem = new OrderItem();
        //设置spuid
        orderItem.setSpuId(sku.getSpuId());
        //设置skuid
        orderItem.setSkuId(sku.getId());
        //商品名
        orderItem.setName(sku.getName());
        //商品单价
        orderItem.setPrice(sku.getPrice());
        //购买数量
        orderItem.setNum(num);
        //商品总价
        orderItem.setMoney(orderItem.getPrice() * num);
        //需要支付的价格
        orderItem.setPayMoney(orderItem.getPrice() * num);
        //设置图片
        orderItem.setImage(sku.getImage());
        //设置重量
        orderItem.setWeight(sku.getWeight() * num);
        //分类信息
        orderItem.setCategoryId1(spu.getCategory1Id());
        orderItem.setCategoryId2(spu.getCategory2Id());
        orderItem.setCategoryId3(spu.getCategory3Id());
        return orderItem;

    }
}
